import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'
import { routesStore, routeStore, nowPageStore } from '@/store/modules/routesInfo'
import { tokenStore,loginStore } from '@/store/modules/user'

// const routes = [
//     {
//         path: '/home',
//         name: 'home',
//         meta: {
//             title: '主页'
//         },
//         component: () => import('@/views/Layout.vue'),
//         children: [
//             {
//                 path: '/homePage',
//                 name: 'homePage',
//                 meta: {
//                     title: '工作台'
//                 },
//                 component: () => import('@/views/home/homePage.vue')
//             },
//             {
//                 path: '/homes',
//                 name: 'homes',
//                 meta: {
//                     title: 'sss'
//                 },
//                 component: () => import('@/views/home/homePage.vue')
//             },
//             {
//                 path: '/user',
//                 name: 'user',
//                 meta: {
//                     title: '用户管理'
//                 },
//                 component: () => import('@/views/user/UserManage.vue')
//             },
//             {
//                 path: '/2-1',
//                 name: 'camera',
//                 meta: {
//                     title: '实时监控'
//                 },
//                 component: () => import('@/views/video/videoTest.vue')
//             },
//             {
//                 path: '/2-2',
//                 name: 'historyVideo',
//                 meta: {
//                     title: '查看历史监控'
//                 },
//                 component: () => import('@/views/video/historyNew.vue')
//             },
//             {
//                 path: '/2-3',
//                 name: 'abnormalBehavior',
//                 meta: {
//                     title: '异常入侵检测'
//                 },
//                 component: () => import('@/views/video/abnormalRecogVideo.vue')
//             },
//             {
//                 path: '/2-4',
//                 name: 'peopleRecognize',
//                 meta: {
//                     title: '人员识别'
//                 },
//                 component: () => import('@/views/video/peopleRecogVideo.vue')
//             },
//             {
//                 path: '/2-5',
//                 name: 'fireRecognize',
//                 meta: {
//                     title: '火灾检测'
//                 },
//                 component: () => import('@/views/video/fireRecogVideo.vue')
//             },
//             {
//                 path: '/3-1',
//                 name: 'trackVideo',
//                 meta: {
//                     title: '路线跟踪'
//                 },
//                 component: () => import('@/views/Track/trackVideo.vue')
//             },
//             {
//                 path: '/3-2',
//                 name: 'trackHistory',
//                 meta: {
//                     title: '查看历史路线'
//                 },
//                 component: () => import('@/views/Track/trackHistory.vue')
//             },
//             {
//                 path: '/4-1',
//                 name: 'historyAlarm',
//                 meta: {
//                     title: '历史警报'
//                 },
//                 component: () => import('@/views/alarm/historyAlarm.vue')
//             },
//             {
//                 path: '/4-2',
//                 name: 'historyAnalyst',
//                 meta: {
//                     title: '历史警报分析'
//                 },
//                 component: () => import('@/views/alarm/historyAnalyst.vue'),
//             },
//             {
//                 path: '/5-1',
//                 name: 'whiteList',
//                 meta: {
//                     title: '管理白名单'
//                 },
//                 component: () => import('@/views/user/WhiteList.vue')
//             },
//             {
//                 path: '/5-2',
//                 name: 'blackList',
//                 meta: {
//                     title: '管理黑名单'
//                 },
//                 component: () => import('@/views/user/BlackList.vue')
//             },
//             {
//                 path: '/5-3',
//                 name: 'informationInput',
//                 meta: {
//                     title: '录入信息'
//                 },
//                 component: () => import('@/views/user/AddResident.vue')
//             },
//             {
//                 path: '/5-4',
//                 name: 'ElderList',
//                 meta: {
//                     title: '老人列表'
//                 },
//                 component: () => import('@/views/user/ElderList.vue')
//             },
//             {
//                 path: '/5-5',
//                 name: 'r',
//                 meta: {
//                     title: 'jj'
//                 },
//                 component: () => import('@/views/login/videoTest.vue')
//             },
//         ]
//     },
//     {
//         path: '/',
//         name: 'login',
//         meta: {
//             title: '用户登录'
//         },
//         component: () => import('@/views/login/Login.vue')
//     },
//     {
//         path: '/regist',
//         name: 'regist',
//         meta: {
//             title: '用户注册'
//         },
//         component: () => import('@/views/login/Regist.vue')
//     },
// ]

const routes = [
    {
        path: '/layout',
        meta: {title: '主页'},
        component: () => import('@/views/Layout.vue'),
        redirect: '/layout/homePage',
        children: [
            {
                path: 'homePage',
                name: 'homePage',
                meta: {title: '主页'},
                component: () => import('@/views/home/homePage.vue')
            },
            {
                path: 'userManage',
                name: 'userManage',
                meta: {title: '用户管理'},
                component: () => import('@/views/user/UserManage.vue')
            },
            {
                path: 'myinfo',
                name: 'MyInfo',
                meta: { title: '个人信息'},
                component: () => import('@/views/myInfo/myInfo.vue')
            },
            {
                path: 'realTime',
                name: 'realTime',
                meta: { title: '实时监控' },
                component: () => import('@/views/video/videoTest.vue')
            },
        ]
    },
    {
        path: '/layout/monitor',
        meta: { title: '监控' },
        component: () => import('@/views/Layout.vue'),
        children: [
            {
                path: 'seamless',
                name: 'seamless',
                meta: { title: '无缝监控' },
                component: () => import('@/views/video/SeamlessVideo.vue')
            },
            {
                path: 'history',
                name: 'history',
                meta: { title: '查看历史监控' },
                component: () => import('@/views/video/historyNew.vue')
            },
            {
                path: 'abnormal',
                name: 'abnormal',
                meta: { title: '异常入侵检测' },
                component: () => import('@/views/video/abnormalRecogVideo.vue')
            },
            {
                path: 'personnel',
                name: 'personnel',
                meta: { title: '人员识别' },
                component: () => import('@/views/video/peopleRecogVideo.vue')
            },
            // {
            //     path: 'fire',
            //     name: 'fire',
            //     meta: { title: '火灾检测' },
            //     component: () => import('@/views/video/fireRecogVideo.vue')
            // },
        ],
    },
    {
        path: '/layout/track',
        meta: { title: '路线' },
        component: () => import('@/views/Layout.vue'),
        children: [
            {
                path: 'trackVideo',
                name: 'trackVideo',
                meta: { title: '路线跟踪' },
                component: () => import('@/views/Track/trackVideo.vue')
            },
            {
                path: 'trackHistory',
                name: 'trackHistory',
                meta: { title: '查看历史路线' },
                component: () => import('@/views/Track/trackHistory.vue')
            },
        ],
    },
    {
        path: '/layout/alert',
        meta: { title: '警报' },
        component: () => import('@/views/Layout.vue'),
        children: [
            {
                path: 'historyAlarm',
                name: 'historyAlarm',
                meta: { title: '历史警报' },
                component: () => import('@/views/alarm/historyAlarm.vue')
            },
            {
                path: 'historyAnalyst',
                name: 'historyAnalyst',
                meta: { title: '历史警报分析' },
                component: () => import('@/views/alarm/historyAnalyst.vue'),
            },
        ],
    },
    {
        path: '/layout/user',
        meta: { title: '人员' },
        component: () => import('@/views/Layout.vue'),
        children: [
            {
                path: 'whitelist',
                name: 'whitelist',
                meta: { title: '管理白名单'},
                component: () => import('@/views/user/WhiteList.vue')
            },
            {
                path: 'blacklist',
                name: 'blacklist',
                meta: { title: '管理黑名单'},
                component: () => import('@/views/user/BlackList.vue')
            },
            {
                path: 'input',
                name: 'informationInput',
                meta: { title: '录入信息'},
                component: () => import('@/views/user/AddResident.vue')
            },
            {
                path: 'elderly',
                name: 'ElderList',
                meta: { title: '老人列表'},
                component: () => import('@/views/user/ElderList.vue')
            },
            {
                path: 'caregiver',
                name: 'CaregiverList',
                meta: { title: '护工列表'},
                component: () => import('@/views/user/CaregiverList.vue')
            },
            {
                path: 'personnel',
                name: 'UserAnalyst',
                meta: { title: '人员统计'},
                component: () => import('@/views/user/UserAnalyst.vue')
            },
            {
                path: 'health/:id',
                name: 'HealthInfo',
                meta: { title: '健康数据'},
                component: () => import('@/views/health/HealthShow.vue')
            },
        ],
    },
    { path: '/', redirect: '/login' },
    {
        path: '/login',
        name: 'login',
        meta: { title: '用户登录' },
        component: () => import('@/views/login/Login.vue')
    },
    {
        path: '/register',
        name: 'register',
        meta: { title: '用户注册' },
        component: () => import('@/views/login/Regist.vue')
    },
]


const router = createRouter({
    history: createWebHashHistory(),
    routes: routes,
})

export default router

// 2. 路由发生变化修改页面 title
router.beforeEach((to, from, next) => {
    let arr = routes[0].children?.map((item) => {
        // 这段代码是 当前菜单栏的词条
        return Object.assign({}, { title: item.meta.title, path: item.path })
    })

    routesStore().setRoutes(arr as any)

    if (to.meta.title) {
        document.title = to.meta.title as string
        nowPageStore().setNowPage(to.meta.title as string)

        if (!routeStore().globalRoute.some(item => item.title === document.title) && document.title !== '用户登录' && document.title !== '用户注册') {
            // 添加控制台
            console.log('此时，已经跳转要失败了！')
            routeStore().pushRoute({ title: document.title, path: to.path })
        }
    }

    // 取消 token 和登录状态检查
    // const userStore = tokenStore();
    // const loginStatus = loginStore();

    // if (userStore.token) {
    //     if (loginStatus.isLogin) {
    //         next();
    //     } else {
    //         next('/');
    //     }
    // } else {
    // if (to.name === 'login' || to.name === 'regist') {
        next();
    // } else {
    //     next();
    // }
    // }
})
